package com.cy.sys.serviceImpl;

import com.cy.common.excetion.ServiceException;
import com.cy.sys.dao.logDao;
import com.cy.sys.pojo.PageObject;
import com.cy.sys.pojo.sysLog;
import com.cy.sys.service.logService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class LogServiceImpl implements logService {

    @Autowired
    private logDao dao;



    @Override
    public PageObject findPageObjects(String username, Long pageCurrent) {
        System.out.println(pageCurrent+"sadasdsadsa");

           if(pageCurrent<1 || pageCurrent==null)
               throw new IllegalArgumentException("页码不正确");
            Long rowCount=dao.getRowCount(username);
            if(rowCount==0)
                throw new IllegalArgumentException("查无此人");
        int pageSize=10;
        Long startIndex=(pageCurrent-1)*pageSize;
        List<sysLog> records=dao.findPageObjects(username,startIndex,pageSize);



        return new PageObject(pageCurrent,pageSize,rowCount,records);
    }

    @Override
    public int doDeleteObjects(Integer... ids) {
        System.out.println(ids.length);
        if(ids==null || ids.length<=0)
            throw new IllegalArgumentException("未选择");
        int a=dao.doDeleteObjects(ids);
        System.out.println(a+"wkslsls");
        if(a==0)
            throw new ServiceException("该记录可能已经消失");

        return a;
    }

    @Override
    @Async//异步操作的方法
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor =Exception.class)
    public void saveObject(sysLog sysLog){
       int a = dao.saveObject(sysLog);
       if(a==0)
           throw new IllegalArgumentException("写入错误");

    }
}

